import os
from .dev import DevConfig
from .test import TestConfig
from .pro import ProConfig


config_map = {
    'dev': DevConfig,
    'test': TestConfig,
    'pro': ProConfig
}


class SystemEnvironment:
    """读取项目根目录下打env.py配置文件, 根据APPLICATION_ENV导入对应的配置"""
    def __init__(self, app=None):
        self.app = app
        if app is not None:
            self.init_app()

    def init_app(self):
        env_file = os.path.join(self.app.root_path, 'env.py')
        if not os.path.exists(env_file):
            return
        self.app.config.from_pyfile(env_file)
        app_env = self.app.config.get('APPLICATION_ENV', 'dev')
        if app_env in config_map:
            self.app.config.from_object(config_map[app_env])